package com.wanban.adutils.advertisers.admob

import android.app.Activity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.wanban.adutils.AdUtils
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants
import com.wanban.adutils.tools.AdmobViewTools


class AdmobNativeAdUtils : BaseAdLoader() {

    private var nativeAd: NativeAd? = null


    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        val videoOptions = VideoOptions.Builder()
            .setStartMuted(true)
            .build()
        val nativeBuilder = NativeAdOptions.Builder()
            .setVideoOptions(videoOptions)
            .build()
        val adLoader = AdLoader.Builder(activity, realUnitId)
            .forNativeAd {
                if (isTimeout) {
                    destroy()
                    return@forNativeAd
                }
                setAdLoadSuccess()
                sdkLoadFinish()
                nativeAd = it
                callback.onAdLoaded(it, adInfo.ad_duration)
            }
            .withAdListener(object : AdListener() {
                override fun onAdFailedToLoad(errorCode: LoadAdError) {
                    super.onAdFailedToLoad(errorCode)
                    if (isTimeout) {
                        destroy()
                        return
                    }
                    sdkLoadFinish()
                    val code = getAdmobErrorCode(errorCode)
                    analysisErrorCode(adInfo, code)
                    callback.onAdLoadFailed(code)
                    destroy()
                }

                override fun onAdClosed() {
                    super.onAdClosed()
                    callback.onAdComplete()
                }

                override fun onAdClicked() {
                    super.onAdClicked()
                    callback.onAdComplete()
                }

                override fun onAdImpression() {
                    super.onAdImpression()
                    callback.onAdOpened()
                }
            })
            .withNativeAdOptions(nativeBuilder)
            .build()
        val adRequest = AdUtils.getAdRequest()
        adLoader.loadAd(adRequest)
    }



//    override fun loadAd(activity: Activity?, adInfo: AdInfo, callback: OnLoadAdCallback)  {
//        super.loadAd(activity, adInfo, callback)
//        val videoOptions = VideoOptions.Builder()
//                .setStartMuted(true)
//                .build()
//        val nativeBuilder = NativeAdOptions.Builder()
//                .setVideoOptions(videoOptions)
//                .build()
//
//        val adLoader = AdLoader.Builder(activity!!, realUnitId)
//            .forNativeAd {
//                sdkLoadFinish()
//
//                nativeAd = it
//                callback.onAdLoaded(it, adInfo.ad_duration)
//            }
//            .withAdListener(object : AdListener() {
//                    override fun onAdFailedToLoad(errorCode: LoadAdError) {
//                        super.onAdFailedToLoad(errorCode)
//                        val code = getAdmobErrorCode(errorCode)
//                        analysisErrorCode(adInfo, code)
//                        Log.d(AdConstants.TAG, "Admob Native onAdFailedToLoad -> " + errorCode.responseInfo?.mediationAdapterClassName)
//                        sdkLoadFinish()
//                        mOnLoadAdCallback?.onAdLoadFailed(code)
//                        destroy()
//                    }
//
//                    override fun onAdClosed() {
//                        super.onAdClosed()
//                        mOnLoadAdCallback?.onAdComplete()
//                    }
//
//                    override fun onAdClicked() {
//                        super.onAdClicked()
//                        mOnLoadAdCallback?.onAdComplete()
//                    }
//
//                    override fun onAdImpression() {
//                        super.onAdImpression()
//                        mOnLoadAdCallback?.onAdOpened()
//                    }
//                })
//                .withNativeAdOptions(nativeBuilder)
//                .build()
//        val adRequest = AdUtils.getAdRequest()
//        adLoader.loadAd(adRequest)
//    }


    override fun showAd(adRoot: ViewGroup, type: Int) {
        if (nativeAd == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INVALID_REQUEST)
            destroy()
            return
        }
        adRoot.visibility = View.VISIBLE
        when (type) {
            AdConstants.ADTYPE_OPEN -> {
                AdmobViewTools.showOpenAd(adRoot, nativeAd!!)
                countDownOpenAd(adRoot)
            }

            AdConstants.ADTYPE_PAUSE_VIDEO -> {
                AdmobViewTools.showPauseVideoAd(adRoot, nativeAd!!, mOnLoadAdCallback)
            }

            AdConstants.ADTYPE_INSTREAM_VIDEO -> {
                AdmobViewTools.showInstreamAd(adRoot, nativeAd!!)
                // 开启倒计时
                countDownInstreamAd(adRoot)
            }

            else -> {
                mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_AD_LOADED_CAN_NOT_SHOW)
                destroy()
            }
        }
    }

    override fun destroy() {
        super.destroy()
        nativeAd?.destroy()
        nativeAd = null
    }
}